package com.wisewin.circle.api.controller.base;

import com.aliyuncs.utils.StringUtils;
import com.wisewin.circle.config.ParamException;
import com.wisewin.circle.entity.LoginUser;
import com.wisewin.circle.entity.db.OrgUserDO;
import com.wisewin.circle.service.UserService;
import com.wisewin.circle.system.Constants;
import com.wisewin.circle.util.JedisUtil;
import com.wisewin.circle.util.JsonUtils;
import com.wisewin.circle.util.PageUtil;
import com.wisewin.circle.util.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class BaseController {

    @Autowired
    private UserService userService;

    // 后台管理员用户登录
    public String setOrgLoginUser(OrgUserDO user) {
        String sessionID = "orguser_" + UUIDUtils.UUIDGenerator();
        // 将登录对象存进redis中
        JedisUtil.setEx(sessionID , JsonUtils.getJsonString4JavaPOJO(user) , Constants.ONE_DAY_LONG);
        return sessionID;
    }

    // 获取后台管理员用户登录信息
    public OrgUserDO getOrgLoginUser(String sessionId) {
        String userjson = JedisUtil.get(sessionId);
        return (OrgUserDO)JsonUtils.getObject4JsonObject(JsonUtils.getJsonObject4JavaPOJO(userjson), OrgUserDO.class);
    }


    /**
     * 普通用户的登录信息存进redis
     * * 注意 ： 这是普通用户，不是机构用户
     */
    public String setLoginUser ( LoginUser user) {
        String sessionID = "user_" + UUIDUtils.UUIDGenerator();
        // 将登录对象存进redis中
        JedisUtil.setEx(sessionID , JsonUtils.getJsonString4JavaPOJO(user), Constants.THREE_MONTH_LONG);
        return sessionID;
    }

    /**
     * 获取登录用户对象  （普通用户）
     * @return
     */
    public LoginUser getLoginUser(String sessionId) {
        String userjson = JedisUtil.get(sessionId);
        return (LoginUser)JsonUtils.getObject4JsonObject(JsonUtils.getJsonObject4JavaPOJO(userjson), LoginUser.class);
    }

    /**
     * 镜子端退出登录
     * @return
     */
    public void loginOutForMirror(String sessionId) {
        JedisUtil.del(sessionId);
    }

    //验证参数
    public void verification(Object... obj) throws ParamException {
        if (obj == null || obj.length < 1) {
            throw new ParamException("必传参数未传");
        }
        for (Object o : obj) {
            if (o instanceof String) {
                if (StringUtils.isEmpty((String) o)) {
                    throw new ParamException("必传参数未传");
                }
            } else {
                if (o == null) {
                    throw new ParamException("必传参数未传");
                }
            }
        }
    }

    public String getCookie(HttpServletRequest request , String key) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null || cookies.length == 0) {
            return null ;
        }
        for(Cookie c :cookies ){
            if (c.getName().equals(key)) {
                return c.getValue() ;
            }
        }
        return null ;
    }

    public void removeCookie (HttpServletRequest request , HttpServletResponse response , String key) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null || cookies.length == 0) {
            return  ;
        }
        for(Cookie c :cookies ){
            if (c.getName().equals(key)) {
                c.setMaxAge(0);
                c.setValue(null);
                c.setPath("/");
                response.addCookie(c);
            }
        }
    }

    public void setCookie(HttpServletResponse response, String key , String value , int expiry) {
        Cookie cookie = new Cookie(key, value);
        cookie.setMaxAge(expiry); //365 * 24 * 60 * 60
        cookie.setPath("/");
        response.addCookie(cookie);
    }

    /**
     * 用户登录
     * @Param mobile 账号
     * @Param password 密码
     */
//    public String loginUser(String mobile, String password, HttpServletRequest request) {
//        //使用账户和密码生成token
//        TokenUtil tokenUtil = new TokenUtil();
//        String token = tokenUtil.getToken(mobile, password);
//        //拿到token以后，存入redis，并保存ip，有效期一周
//        Map<String, String> redisMap = new HashMap<>();
//        redisMap.put("token", token);
//        IpUtil ipUtil = new IpUtil();
//        redisMap.put("ip", ipUtil.getIpAddr(request));
//        //账号为key token和ip为value 保存一周
//        redis.set("user" + mobile, redisMap, 60 * 60 * 24 * 7);
//        return token;
//    }


    private Long default_page_size = 10L;
    private Long default_page_no = 1L;

    public PageUtil getPageQuery(Long pageNo, Long pageSize) {
        if (null == pageNo) {
            pageNo = default_page_no;
        }
        if (null == pageSize) {
            pageSize = default_page_size;
        }
        if (null != pageNo) {
            PageUtil pageUtil = new PageUtil((pageNo - 1) * pageSize, pageSize);
            return pageUtil;
        }
        return null;
    }
}
